﻿//最后更新日期：2011.5.31
/**
*显示器
**/
function Monitor(ctrl, passStyle, invalidStyle) {

    if (ctrl) {
        this.OriginalMsg = ctrl.innerHTML;
        this.OriginalStyle = ctrl.className;
    }
    else {
        ctrl = {};
    } 
    this.Control = ctrl;
    this.PassStyle = passStyle;
    this.InvalidStyle = invalidStyle;
}

/*显示通过的信息*/
Monitor.prototype.PassShow = function (message) {
    this.Message = message;
    this.Control.className = this.PassStyle;
    this.Show();
};

/*显示失败的信息*/
Monitor.prototype.InvalidShow = function (message) {
    this.Message = message;
    this.Control.className = this.InvalidStyle;
    this.Show();
};

/*显示信息*/
Monitor.prototype.Show = function () {
    this.Control.innerHTML = this.Message;
};

/*还原显示器*/
Monitor.prototype.Revert = function () {
    this.Message = this.OriginalMsg;
    this.Control.className = this.OriginalStyle;
    this.Show();
};

/**
*验证组件
**/
function Component(preventMsg, engine) {
    this.preventMessage = preventMsg;
    this.engine = engine;
};

/**
*验证器
**/
function Verification(ctrl, m, r) {
    this.control = ctrl;
    this.monitor = m;
    this.regular = r;
    this.max = 0;
    this.min = 0;
    this.empty = false;
    this.passMsg = '';

    /*验证组件仓库*/
    this.module = [];

    /*非空组件*/
    this.testEmpty = new Component('', function () {
        if (this.value() == '') {
            this.valid = false;
            this.monitor.InvalidShow(this.testEmpty.preventMessage);
        }
    });

    /*最大长度验证组件*/
    this.testMaxLength = new Component('', function () {
        if (this.value().length > this.max) {
            this.valid = false;
            this.monitor.InvalidShow(this.testMaxLength.preventMessage);
        }
    });

    /*最小长度组件*/
    this.testMinLength = new Component('', function () {
        if (this.value().length < this.min) {
            this.valid = false;
            this.monitor.InvalidShow(this.testMinLength.preventMessage);
        }
    });

    /*验证是否匹配格式*/
    this.testRegular = new Component('', function () {
        if (!this.regular.test(this.value())) {
            this.valid = false;
            this.monitor.InvalidShow(this.testRegular.preventMessage);
        }
    });
}

/*验证组件初始化*/
Verification.prototype.initialize = function () {
    if (!this.empty) {
        this.module.push('testEmpty');
    }
    if (this.max) {
        this.module.push('testMaxLength');
    }
    if (this.min) {
        this.module.push('testMinLength');
    }
    if (this.regular) {
        this.module.push('testRegular');
    }
    if (this.custom) {
        this.module = this.module.concat(this.custom);
    }
};

/*添加扩展验证组件*/
Verification.prototype.expand = function (verify) {
    if (!this.custom) this.custom = [];
    for (var i = 0; i < arguments.length; i++) {
        this[arguments[i].key] = arguments[i].core;
        this.custom.push(arguments[i].key);
    }
};

/*获取验证对象的值*/
Verification.prototype.value = function () {
    return this.control.value;
};

/*验证对象定时器*/
Verification.prototype.timer = function (timeSpan) {
    var temp = this;
    this.TID = setTimeout(function () { temp.check(); }, timeSpan || 2000);
};

/*取消定时的验证*/
Verification.prototype.clearTimer = function () {
    if (this.TID) {
        clearTimeout(this.TID);
        this.TID = false;
    }
};

/*验证*/
Verification.prototype.check = function () {
    this.valid = true;
    this.initialize();
    for (var i = 0; i < this.module.length; i++) {
        this[this.module[i]].engine.apply(this);
        if (!this.valid) return;
    }
    this.monitor.PassShow(this.passMsg||"&nbsp;");
};

/**
*验证器创建工厂
**/
Verification.Create = function (ctrl, m, r) {
    return new Verification(ctrl, m, r);
};

/******************************************************/
/*表单类*/
function form() {
    this.error = { count: 0 };
    this.controls = {};
    this.elements = {};
    for (var i = 0; i < arguments.length; i++) {
        this.elements[arguments[i][0]] = arguments[i][1];
    }
}


/*验证表单*/
form.prototype.check = function () {
    if (this.error.count > 0) return false;
    var isPass = false;
    for (var i in this.elements) {
        if (!this.controls[i])
            this.elements[i]();
        this.controls[i].check();
        if (!(isPass = this.controls[i].valid)) {
            break;
        }
    }
    return isPass;
};
